<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>
        <a href="/">用户管理</a>
      </el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="7">
          <div class="grid-content bg-purple">
            <!-- 搜索框 -->
            <el-input
              placeholder="请输入内容"
              class="input-with-select"
              v-model="queryData.query"
              clearable
              @change="getUsersList"
            >
              <el-button slot="append" icon="el-icon-search" @click="getUsersList"></el-button>
            </el-input>
          </div>
        </el-col>
        <el-col :span="4">
          <div class="grid-content bg-purple">
            <el-button type="primary" @click="goAddPage">添加商品</el-button>
          </div>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <el-table :data="goodsList" border style="width: 100%">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量"></el-table-column>
        <el-table-column prop="add_time" label="添加时间"></el-table-column>
        <el-table-column  label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeGoods(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
        <!-- 分页功能 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryData.pagenum"
        :page-sizes="[1, 3, 5, 6]"
        :page-size="queryData.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  data () {
    return {
      // 输入框数据
      queryData: {
        query: '',
        pagenum: 1,
        pagesize: 3
      },
      goodsList: [],
      total: undefined
    }
  },
  mounted () {
    this.getUsersList()
  },
  methods: {
    // 获取所有商品数据
    async getUsersList () {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryData
      })
      if (res.meta.status !== 200) return this.$message.error('获取数据失败')
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    // 点击选择不同条数，重新获取数据渲染列表
    handleSizeChange (val) {
      this.queryData.pagesize = val
      this.getUsersList()
    },
    // 点击选择不同页数，重新获取数据渲染列表
    handleCurrentChange (val) {
      this.queryData.pagenum = val
      this.getUsersList()
    },
    // 删除商品
    async removeGoods (id) {
      try {
        const str = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        if (str !== 'confirm') return
        const { data: res } = await this.$http.delete('goods/' + id)
        if (res.meta.status !== 200) return this.$message.error('删除商品失败')
        this.$message.success('删除商品成功')
        this.getUsersList()
      } catch (error) {
        this.$message.error('取消删除')
      }
    },
    goAddPage () {
      this.$router.push('/add')
    }
  }
}
</script>
